<?php
include ("./src/jpgraph.php");
include ("./src/jpgraph_line.php");
include ("./src/jpgraph_bar.php");

$d = unserialize(stripslashes(urldecode($_GET['d'])));
$d2 = unserialize(stripslashes(urldecode($_GET['d2'])));
$d3 = unserialize(stripslashes(urldecode($_GET['d3'])));
$l = unserialize(stripslashes(urldecode($_GET['l'])));

$mid = round(array_sum($d2) / count($d2));
for($i=0;$i<count($d2); $i++)
{
    $d4[] = $mid;
}
//$d4[] = $mid;

$graph = new Graph(800,300,"auto");    
$graph->SetScale("textlin",0,(max($d)*1.3));
$graph->SetYScale(0, "lin", 0, (max($d3)*1.2));
$graph->SetYScale(1, "lin", 0, (max($d2)*1.2));
$graph->SetYScale(2, "lin", 0, (max($d2)*1.5));
$graph->SetMargin(50,90,75,45);

//$graph->Set90AndMargin(100,10,80,10); 

$graph->SetBackgroundGradient('#ffffff','gray9',GRAD_HOR,BGRAD_PLOT);

$graph->title->SetColor("gray3"); 
$graph->SetShadow(FALSE);
$graph->SetFrame(FALSE);

//$graph->xgrid->SetFill(true,'#dddddd@0.5','#f0f0f0@0.5');
$graph->ygrid->SetLineStyle('dashed');
$graph->ygrid->SetColor('gray');
$graph->xgrid->Show();
$graph->xgrid->SetLineStyle('dashed');
$graph->xgrid->SetColor('gray');
$graph->tabtitle->Set($_GET['t']);
$graph->tabtitle->SetColor('gray3');
$graph->tabtitle->SetFillColor('white');
$graph->legend->Pos(0.01,0.01);
$graph->legend->SetColumns(2);
$graph->xaxis->SetTickLabels($l);
//$graph->xaxis->SetLabelAngle(90); 

$pline = new PlotLine(HORIZONTAL, (round(array_sum($d)/count($d))),'plum4',1);
$pline->SetLegend('('.round(array_sum($d)/count($d), 2).') Average Subscription Counts');
$graph->Add($pline);


$bplot = new BarPlot($d);
$bplot->SetFillColor('plum4@0.3');
$bplot->SetWidth(0.5);
$bplot->value->Show();
$bplot->SetLegend("Subscription Counts");
$bplot->value->SetFormat('%d');
$bplot->SetValuePos('bottom');

$graph->yaxis->SetColor("plum4");
$graph->yaxis->title->Set("Subscription Counts");
$graph->yaxis->title->SetColor("plum4");

$p3 = new LinePlot($d3); 
$p3->SetColor('red@0.6'); 
$p3->SetWeight(2);
$p3->SetLegend("Average Activity");
$p3->SetBarCenter();
$p3->mark->SetType(MARK_DIAMOND);
$p3->mark->SetFillColor("red@0.5");
//$p3->SetFillGradient('red@0.8','red@0.99'); 
$p3->mark->SetColor("red:0.5");
$p3->mark->SetWidth(3);
$graph->AddY(0,$p3); 

$l2plot=new LinePlot($d2);
$l2plot->SetColor("blue:1");
$l2plot->SetWeight(1);
$l2plot->SetFillColor('navy@0.9');
$l2plot->SetLegend("Total Sales");
//$l2plot->SetBarCenter();
$l2plot->value->Show();
$l2plot->value->SetColor('navy');
$l2plot->value->SetFont(FF_FONT0, FS_NORMAL);
$l2plot->mark->SetType(MARK_FILLEDCIRCLE);
$l2plot->mark->SetFillColor("blue");
$l2plot->mark->SetWidth(4);
$l2plot->value->SetFormat('$%d');
$graph->AddY(1, $l2plot);


$l3=new LinePlot($d4);
$l3->SetColor("darkgreen");
$l3->SetWeight(1);
//$l3->SetFillColor('navy@0.9');
$l3->SetLegend("(\$$mid) Average sale");
//$l2plot->SetBarCenter();
//$l3->value->Show();
//$l3->value->SetColor('darkgreen');
//$l3->value->SetFont(FF_FONT0, FS_NORMAL);
//$l3->mark->SetType(MARK_FILLEDCIRCLE);
//$l3->mark->SetFillColor("blue");
//$l3->mark->SetWidth(4);
//$l3->value->SetFormat('$%d');
$graph->AddY(2, $l3);


$graph->ynaxis[0]->SetColor('blue'); 
$graph->ynaxis[0]->title->Set("Subscription Total Payment");
$graph->ynaxis[0]->title->SetColor("blue");
$graph->ynaxis[1]->SetColor('red'); 
$graph->ynaxis[1]->title->Set("Average Activity");
$graph->ynaxis[1]->title->SetColor("red");
//$graph->y2axis->title->Set("Subscription Total Payment");
//$graph->SetY2Scale('lin',0,(max($d2)*1.2)); 
//$graph->y2axis->SetColor("blue");
//$graph->y2axis->title->Set("Subscription Total Payment");
//$graph->y2axis->title->SetColor("blue");


$graph->Add($bplot);



//$pline2 = new PlotLine(HORIZONTAL, (round(array_sum($d2)/count($d2))),'green',1);
//$pline2->SetLegend('Total Average Level Of Sales 2');
//$graph->Add($bplot);

$graph->Stroke();
?>
